//
//  YSOrderBusinessHeaderView.m
//  Yueshi
//
//  Created by Northwan on 15/8/3.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSOrderBusinessHeaderView.h"
@interface YSOrderBusinessHeaderView()
@property (weak, nonatomic) IBOutlet UIImageView *logoView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *payStatusLabel;
@property (weak, nonatomic) IBOutlet UIImageView *arrowIcon;
@property (strong, nonatomic) NSArray *iconsArray;

@end
@implementation YSOrderBusinessHeaderView

+(instancetype)headerView{
    return [[[NSBundle mainBundle ]loadNibNamed:@"YSOrderBusinessHeaderView" owner:nil options:nil]lastObject];
}

-(void)setOrder:(YSOrder *)order{
    _order = order;
    [self.logoView setImageURLStr:order.logo];
    [self.nameLabel setText:order.supplierName];
    [self.payStatusLabel setText:order.orderStatusName];
}

-(void)setSupplier:(YSSupplier *)supplier{
    _supplier = supplier;
    [self.logoView setImageURLStr:supplier.logo];
    [self.nameLabel setText:supplier.supplierName];
    self.payStatusLabel.hidden = YES;
}

-(void)setOrderWithSupplierIcons:(YSOrder *)order supplierIcons:(NSArray *)supplierIcons{
    
    _iconsArray = supplierIcons;
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
   
    for (int i = 0; i<supplierIcons.count; i++) {
        if(i < 4){
            UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.logoView.size.width, self.logoView.size.height)];
            icon.clipsToBounds = YES;
            [icon setImageURLStr:supplierIcons[i]];
            icon.layer.cornerRadius = icon.width*0.5;
            icon.y = (self.height-icon.height)*0.5;
            [self addSubview:icon];
        }
    }
    
    if(self.subviews.count >= 4){
        //添加三个点的图标
        UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.logoView.size.width, self.logoView.size.height)];
        icon.clipsToBounds = YES;
        [icon setImage:[UIImage imageNamed:@"business_more"]];
        [icon sizeToFit];
        icon.layer.cornerRadius = icon.width*0.5;
        icon.y = (self.height-icon.height)*0.5;
        [self addSubview:icon];
    }
    
    //支付状态
    CGSize statusSize = [order.orderStatusName sizeWithFont:SYS_FONT(16)];
    UILabel *statusLabel = [[UILabel alloc]init];
    statusLabel.size = statusSize;
    statusLabel.font = SYS_FONT(16);
    statusLabel.textColor = ThemeColor;
    statusLabel.x = SCREENW-YSPublicXInset-statusSize.width;
    statusLabel.y = (self.height-statusSize.height)/2;
    [self addSubview:statusLabel];
    [statusLabel setText:order.orderStatusName];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(YSPublicXInset, self.height-0.5, SCREENW-YSPublicXInset*2, 0.5)];
    [lineView setBackgroundColor:HEXColor(@"#eaeae9")];
    [self addSubview:lineView];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    if(self.iconsArray.count){
        NSArray *subviews = self.subviews;
        for (int i = 0 ;i<subviews.count ; i++) {
            if([subviews[i] isKindOfClass:[UIImageView class]]){
                UIImageView *icon =  subviews[i];
                if(i == 0){
                    icon.x = YSPublicXInset;
                }else{
                    icon.x = CGRectGetMaxX(((UIButton *)subviews[i-1]).frame)+5;
                }
            }
        }
    }
}

@end
